package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.PrizeDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/1/30
 */
@Mapper
public interface PrizeMapper {


    @Insert("INSERT INTO prize (create_id,name, image_url, price, description) " +
            "VALUES (#{createId},#{prizeDO.name}, #{prizeDO.imageUrl}, #{prizeDO.price}, #{prizeDO.description})")
    /** useGeneratedKeys：是否使用数据库的自动生成主键功能，
     *                    设置为 true 时，MyBatis 会尝试从
     *                    数据库获取自动生成的主键值
     *  keyProperty：指定了 Java 对象中用于接收自动生成主键值的属性名
     *  keyColumn：指定了数据库表中自动生成主键的列名
     */
    @Options(useGeneratedKeys = true, keyProperty = "prizeDO.id", keyColumn = "id")
    int createPrize(@Param("createId") Integer createId, @Param("prizeDO") PrizeDO prizeDO);

    @Select("select count(*) from prize where create_id = #{userId}")
    int count(@Param("userId") Integer userId);

    @Select("select * from prize where create_id = #{userId} order by id desc limit #{offset},#{pageSize}")
    List<PrizeDO> selectPrizeListByPage(@Param("userId") Integer userId,
                                        @Param("offset") Integer offset,
                                        @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            " select id from prize" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from prize" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<PrizeDO> batchSelectByIds(@Param("items") List<Long> ids);

    @Select("select * from prize where id = #{id}")
    PrizeDO selectById(@Param("id") Long id);
}
